package com.insyte.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.insyte.R;
import com.insyte.fragments.ProfileFavourisFragment;
import com.insyte.model.InteresetObject;

import java.util.List;

/**
 * Created by Ngo Khanh on 26/6/2015.
 */
public class MyInterestAdapter extends BaseAdapter {

    private final List<InteresetObject> itemList;
    private final LayoutInflater inflater;
    private ProfileFavourisFragment.OnMyInterestItemListener listener;
    public MyInterestAdapter(Context context, List<InteresetObject> itemList,ProfileFavourisFragment.OnMyInterestItemListener itemListener) {
        this.inflater = LayoutInflater.from(context);
        this.itemList = itemList;
        this.listener = itemListener;
    }

    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public Object getItem(int i) {
        return itemList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        View resultView = view;
        ViewHolder holder;
        if (resultView == null) {
            resultView = inflater.inflate(R.layout.row_my_interest, null); //TODO change layout id
            holder = new ViewHolder();

            holder.btn_remove= (Button) resultView.findViewById(R.id.btn_remove); //TODO change view id
            holder.textLabel = (TextView) resultView.findViewById(R.id.txt_my_interest); //TODO change view id
            resultView.setTag(holder);
        } else {
            holder = (ViewHolder) resultView.getTag();
        }
        final InteresetObject item = itemList.get(i);
        holder.btn_remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (listener!=null){
                    listener.onClick(item);
                }
            }
        });
        holder.textLabel.setText(item.getName());
        return resultView;
    }

    private static final class ViewHolder {
        TextView textLabel;
        Button btn_remove;
    }

}